class DataSystem
  acts_as_wrapped_class :methods => [:get, :exist?, :set, :respond_to?, :log]
  
  def initialize(hub_widget)
    @hub_widget = hub_widget
  end
  
  def get(key)
    key = key.to_s
    wd = @hub_widget.widget_datas.find(:first, :conditions => {:key => key})
    wd ? wd.data : nil
  end
  
  def exist?(key)
    key = key.to_s
    wd = @hub_widget.widget_datas.find(:first, :conditions => {:key => key})
    wd ? true : false    
  end
  
  def set(key, value)
    key = key.to_s
    begin
      wd = @hub_widget.widget_datas.find(:first, :conditions => {:key => key})
      if wd
        wd.data = value
        return wd.save
      else
        wd = @hub_widget.widget_datas.create(:key => key, :data => value)
        return wd ? true : false
      end
    rescue
      logger.warn "Exception setting data: #{$!} #{$!.backtrace.join("    ")}"
      false
    end
  end
  
  def log(msg)
    @hub_widget.logger.debug("SANDBOX: #{msg}")
  end
end